/**
 * 
 */
package GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JComboBox;

import Entity.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author aviat
 *
 */
public class ChangePermission extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JTextField OldPermissiontextField;
	private JLabel iconlabel;
	private JComboBox<String> WorkerFnamecomboBox;
	private JComboBox<String> NewPermissioncomboBox;
	private JButton btnChange;
	private JComboBox<String> workerLnamecomboBox;
	private JLabel lblWorkerId;
	private JComboBox<String> workerIDcomboBox;
	private JButton btnRefresh;
	private JComboBox<String> WorkerAddresscomboBox;
	private JLabel lblWorkerAddress;
	
	private ArrayList<User> m_allUsers;
	private ArrayList<User> m_filteredUsers;
	private Object syncObject;	
	private boolean m_isChangeAllowed;
	private JLabel lblPosition;
	private JComboBox<String> PositioncomboBox;
	
	/**
	 * Constructor
	 */
	public ChangePermission() {
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ChangePermission.class.getResource("/resources/back22.png")));
		WorkerFnamecomboBox = new JComboBox<String>();
		WorkerFnamecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		WorkerFnamecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					synchronized (syncObject) {
						if(isChangeAllowed())
							filterList(WorkerFnamecomboBox.getSelectedItem().toString(),"FNAME");
					}
				}
		});
		WorkerAddresscomboBox = new JComboBox<String>();
		WorkerAddresscomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		WorkerAddresscomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(WorkerAddresscomboBox.getSelectedItem().toString(),"ADDRESS");
				}
			}
		});
		
		btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						OldPermissiontextField.setText("");
						filterList(null,"ALL");
					}
				}
			}
		});
			
		NewPermissioncomboBox = new JComboBox<String>();
		NewPermissioncomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						if(NewPermissioncomboBox.getSelectedItem().toString().equals("Worker  "))
							PositioncomboBox.setEnabled(true);
						else
							PositioncomboBox.setEnabled(false);
					}
				}
			}
		});
		NewPermissioncomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		AutoCompletion.enable(this.NewPermissioncomboBox);
		NewPermissioncomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Manager", "Worker  ", "Secretary"}));	
		
		btnChange = new JButton("Change");
		btnChange.setIcon(new ImageIcon(ChangePermission.class.getResource("/resources/change.png")));
		OldPermissiontextField = new JTextField();
		OldPermissiontextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		OldPermissiontextField.setEditable(false);
		workerLnamecomboBox = new JComboBox<String>();
		workerLnamecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		workerLnamecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(workerLnamecomboBox.getSelectedItem().toString(),"LNAME");
				}
			}
		});
		workerIDcomboBox = new JComboBox<String>();
		workerIDcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		workerIDcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
					{
						filterList(workerIDcomboBox.getSelectedItem().toString(),"UNAME");
					}
				}
			}
		});
		PositioncomboBox = new JComboBox<String>();
		
		initialize();
	}

	private void initialize() {
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		JLabel lblChangePermisson = new JLabel("Change Permisson");
		lblChangePermisson.setForeground(new Color(0, 204, 255));
		lblChangePermisson.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblChangePermisson.setBounds(10, 11, 150, 33);
		add(lblChangePermisson);
		
		JLabel lblWorkerLastName = new JLabel("Worker Last Name");
		lblWorkerLastName.setForeground(Color.WHITE);
		lblWorkerLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWorkerLastName.setBounds(32, 151, 130, 27);
		add(lblWorkerLastName);
		
		JLabel lblOldPermission = new JLabel("Old Permission");
		lblOldPermission.setForeground(Color.WHITE);
		lblOldPermission.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblOldPermission.setBounds(427, 81, 130, 27);
		add(lblOldPermission);
		
		JLabel lblNewPermission = new JLabel("New Permission");
		lblNewPermission.setForeground(Color.WHITE);
		lblNewPermission.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewPermission.setBounds(427, 151, 130, 27);
		add(lblNewPermission);
		
		
		WorkerFnamecomboBox.setBounds(189, 82, 112, 27);
		AutoCompletion.enable(this.WorkerFnamecomboBox);
		add(WorkerFnamecomboBox);
		
		
		OldPermissiontextField.setBounds(584, 82, 112, 27);
		add(OldPermissiontextField);
		OldPermissiontextField.setColumns(10);
		
		
		NewPermissioncomboBox.setBounds(584, 151, 112, 27);
		AutoCompletion.enable(this.NewPermissioncomboBox);
		add(NewPermissioncomboBox);
		
		
		btnChange.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnChange.setBounds(436, 403, 123, 27);
		add(btnChange);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		workerLnamecomboBox.setBounds(189, 152, 112, 27);
		AutoCompletion.enable(this.workerLnamecomboBox);
		add(workerLnamecomboBox);
		
		JLabel label = new JLabel("Worker First Name");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		label.setBounds(32, 81, 130, 27);
		add(label);
		
		lblWorkerId = new JLabel("Worker ID");
		lblWorkerId.setForeground(Color.WHITE);
		lblWorkerId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWorkerId.setBounds(32, 223, 130, 27);
		add(lblWorkerId);
		
		
		workerIDcomboBox.setBounds(189, 224, 112, 27);
		AutoCompletion.enable(this.workerIDcomboBox);
		add(workerIDcomboBox);
		
		
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.setIcon(new ImageIcon(ChangePermission.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setBounds(270, 403, 123, 27);
		add(btnRefresh);
		
		lblWorkerAddress = new JLabel("Worker Address");
		lblWorkerAddress.setForeground(Color.WHITE);
		lblWorkerAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblWorkerAddress.setBounds(32, 289, 112, 27);
		add(lblWorkerAddress);
		
		
		WorkerAddresscomboBox.setBounds(189, 290, 203, 27);
		add(WorkerAddresscomboBox);
		
		lblPosition = new JLabel("Position");
		lblPosition.setForeground(Color.WHITE);
		lblPosition.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPosition.setBounds(427, 219, 101, 27);
		add(lblPosition);
		
		
		PositioncomboBox.setBounds(584, 223, 112, 27);
		PositioncomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Stills","Magnets","Video"}));	
		PositioncomboBox.setEnabled(false);
		add(PositioncomboBox);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	/**
	 * @return
	 */
	public JTextField getOldPermissiontextField() {
		return OldPermissiontextField;
	}

	/**
	 * @param oldPermissiontextField
	 */
	public void setOldPermissiontextField(JTextField oldPermissiontextField) {
		OldPermissiontextField = oldPermissiontextField;
	}


	/**
	 * @return
	 */
	public JComboBox<String> getNewPermissioncomboBox() {
		return NewPermissioncomboBox;
	}

	/**
	 * @param newPermissioncomboBox
	 */
	public void setNewPermissioncomboBox(JComboBox<String> newPermissioncomboBox) {
		NewPermissioncomboBox = newPermissioncomboBox;
	}

	/**
	 * @return
	 */
	public JButton getBtnChange() {
		return btnChange;
	}

	/**
	 * @param btnChange
	 */
	public void setBtnChange(JButton btnChange) {
		this.btnChange = btnChange;
	}

	/**
	 * @param btnBack
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	public void setUsers(ArrayList<User> allUsers) {
		m_allUsers = allUsers;		
		m_filteredUsers = new ArrayList<User>();
		syncObject = new Object();
		filterList(null,"ALL");
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			m_filteredUsers.clear();
			
			switch(scope)
			{
			case "FNAME":System.out.println("in fname");
				for(User user : m_allUsers)
				{
					if(user.getName().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "LNAME":System.out.println("in lname");
				for(User user : m_allUsers)
				{
					if(user.getLName().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "UNAME":System.out.println("in cid");
				for(User user : m_allUsers)
				{
					if(String.valueOf(user.getUserName()).equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
			case "ADDRESS":System.out.println("in address");
				for(User user : m_allUsers)
				{
					if(user.getAddress().equals(selectedItem))
					{
						m_filteredUsers.add(user);
					}
				}
				break;
			case "ALL":System.out.println("in all");
				m_filteredUsers.addAll(m_allUsers);
				break;
			}
		}
		m_isChangeAllowed = false;
		filterComboBox(WorkerFnamecomboBox,"FNAME");
		filterComboBox(workerLnamecomboBox,"LNAME");
		filterComboBox(workerIDcomboBox,"UNAME");
		filterComboBox(WorkerAddresscomboBox,"ADDRESS");
		if(workerIDcomboBox.getItemCount()==1&& workerLnamecomboBox.getItemCount()==1&&
			WorkerAddresscomboBox.getItemCount()==1&&WorkerFnamecomboBox.getItemCount()==1)
			addWoldPer();
		m_isChangeAllowed = true;

	}
	
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject) {
			
			for(User user : m_filteredUsers)
			{
				switch(scope)
				{
				case "FNAME":
					combo.addItem(user.getName());
					break;
				case "LNAME":
					combo.addItem(user.getLName());
					break;
				case "UNAME":
					combo.addItem(String.valueOf(user.getUserName()));
					break;
				case "ADDRESS":
					combo.addItem(user.getAddress());
					break;
				}
			}
		}
	}
	/**
	 * This method set the chosen  worker old permission
	 */
	public void addWoldPer(){
		
		for(User user : m_filteredUsers){
			if(String.valueOf(user.getUserName()).equals(workerIDcomboBox.getSelectedItem().toString()))
			{
				OldPermissiontextField.setText(user.getPrivilage());
				NewPermissioncomboBox.removeItem(user.getPrivilage());
			}
			break;
		}
	}
	/**
	 * @return the m_isChangeAllowed
	 */
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}

	/**
	 * @return the workerFnamecomboBox
	 */
	public JComboBox<String> getWorkerFnamecomboBox() {
		return WorkerFnamecomboBox;
	}

	/**
	 * @return the workerLnamecomboBox
	 */
	public JComboBox<String> getWorkerLnamecomboBox() {
		return workerLnamecomboBox;
	}

	/**
	 * @return the workerIDcomboBox
	 */
	public JComboBox<String> getWorkerIDcomboBox() {
		return workerIDcomboBox;
	}
	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the m_allUsers
	 */
	public ArrayList<User> getM_allUsers() {
		return m_allUsers;
	}

	/**
	 * @param m_allUsers the m_allUsers to set
	 */
	public void setM_allUsers(ArrayList<User> m_allUsers) {
		this.m_allUsers = m_allUsers;
	}

	/**
	 * @return the m_filteredUsers
	 */
	public ArrayList<User> getM_filteredUsers() {
		return m_filteredUsers;
	}

	/**
	 * @param m_filteredUsers the m_filteredUsers to set
	 */
	public void setM_filteredUsers(ArrayList<User> m_filteredUsers) {
		this.m_filteredUsers = m_filteredUsers;
	}

	/**
	 * @return the m_isChangeAllowed
	 */
	public boolean isM_isChangeAllowed() {
		return m_isChangeAllowed;
	}

	/**
	 * @param m_isChangeAllowed the m_isChangeAllowed to set
	 */
	public void setM_isChangeAllowed(boolean m_isChangeAllowed) {
		this.m_isChangeAllowed = m_isChangeAllowed;
	}

	/**
	 * @param workerFnamecomboBox the workerFnamecomboBox to set
	 */
	public void setWorkerFnamecomboBox(JComboBox<String> workerFnamecomboBox) {
		WorkerFnamecomboBox = workerFnamecomboBox;
	}

	/**
	 * @param workerLnamecomboBox the workerLnamecomboBox to set
	 */
	public void setWorkerLnamecomboBox(JComboBox<String> workerLnamecomboBox) {
		this.workerLnamecomboBox = workerLnamecomboBox;
	}

	/**
	 * @param workerIDcomboBox the workerIDcomboBox to set
	 */
	public void setWorkerIDcomboBox(JComboBox<String> workerIDcomboBox) {
		this.workerIDcomboBox = workerIDcomboBox;
	}

	/**
	 * @return the workerAddresscomboBox
	 */
	public JComboBox<String> getWorkerAddresscomboBox() {
		return WorkerAddresscomboBox;
	}

	/**
	 * @param workerAddresscomboBox the workerAddresscomboBox to set
	 */
	public void setWorkerAddresscomboBox(JComboBox<String> workerAddresscomboBox) {
		WorkerAddresscomboBox = workerAddresscomboBox;
	}

	/**
	 * @return the positioncomboBox
	 */
	public JComboBox<String> getPositioncomboBox() {
		return PositioncomboBox;
	}

	/**
	 * @param positioncomboBox the positioncomboBox to set
	 */
	public void setPositioncomboBox(JComboBox<String> positioncomboBox) {
		PositioncomboBox = positioncomboBox;
	}
}
